home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1457.dms / var1457.adf / Gadgets / Example9.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  250 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Gadgets                     Tulevagen 22       */
  8. /* File:    Example9.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will open a normal window which is connected to the   */
  21. /* Workbench Screen. The window will use all System Gadgets, and will */
  22. /* close first when the user has selected the System gadget Close     */
  23. /* window. Inside the window we have put a Proportional gadget.       */
  24.  
  25.  
  26.  
  27. #include <intuition/intuition.h>
  28.  
  29.  
  30.  
  31. struct IntuitionBase *IntuitionBase;
  32.  
  33.  
  34.  
  35. /* THE PROPORTIONAL GADGET's STRUCTURES: */
  36.  
  37. /* The IntuiText structure: */
  38. struct IntuiText my_text=
  39. {
  40.   1,         /* FrontPen, colour register 1. */
  41.   0,         /* BackPen, colour register 0. */
  42.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  43.              /* change the background. */ 
  44.   -65, 2,    /* LeftEdge, TopEdge. */
  45.   NULL,      /* ITextFont, use default font. */
  46.   "Volume:", /* IText, the text that will be printed. */
  47.   NULL,      /* NextText, no other IntuiText structures. */
  48. };
  49.  
  50.  
  51. /* We need to declare an Image structure for the knob, but since */
  52. /* Intuition will take care of the size etc of the knob, we do not need */
  53. /* to initialize the Image structure: */
  54. struct Image my_image;
  55.  
  56.  
  57. struct PropInfo my_prop_info=
  58. {
  59.   FREEHORIZ|      /* Flags, the knob should be moved horizontally, and */
  60.   AUTOKNOB,       /* Intuition should take care of the knob image. */
  61.   0,              /* HorizPot, start position of the knob. */
  62.   0,              /* VertPot, 0 since we will not move the knob hor. */
  63.   MAXBODY * 1/64, /* HorizBody, 64 steps. */
  64.   0,              /* VertBody, 0 since we will not move the knob hor. */
  65.  
  66.   /* These variables are initialized and maintained by Intuition: */
  67.  
  68.   0,              /* CWidth */
  69.   0,              /* CHeight */
  70.   0, 0,           /* HPotRes, VPotRes */
  71.   0,              /* LeftBorder */
  72.   0               /* TopBorder */
  73. };
  74.  
  75.  
  76. struct Gadget my_gadget=
  77. {
  78.   NULL,            /* NextGadget, no more gadgets in the list. */
  79.   80,              /* LeftEdge, 80 pixels out. */
  80.   30,              /* TopEdge, 30 lines down. */
  81.   200,             /* Width, 200 pixels wide. */
  82.   12,              /* Height, 12 pixels lines heigh. */
  83.   GADGHCOMP,       /* Flags, complement the colours. */
  84.   GADGIMMEDIATE|   /* Activation, our program will recieve a message */
  85.   RELVERIFY,       /* when the user has selected this gadget, and when */
  86.                    /* the user has released it. */ 
  87.   PROPGADGET,      /* GadgetType, a Proportional gadget. */
  88.   (APTR) &my_image,/* GadgetRender, a pointer to our Image structure. */
  89.                    /* (Intuition will take care of the knob image) */
  90.                    /* (See chapter 3 GRAPHICS for more information) */
  91.   NULL,            /* SelectRender, NULL since we do not supply the */
  92.                    /* gadget with an alternative image. */
  93.   &my_text,        /* GadgetText, volume. */
  94.   NULL,            /* MutualExclude, no mutual exclude. */
  95.   (APTR) &my_prop_info, /* SpecialInfo, pointer to a PropInfo structure. */
  96.   0,               /* GadgetID, no id. */
  97.   NULL             /* UserData, no user data connected to the gadget. */
  98. };
  99.  
  100.  
  101.  
  102. /* Declare a pointer to a Window structure: */ 
  103. struct Window *my_window;
  104.  
  105. /* Declare and initialize your NewWindow structure: */
  106. struct NewWindow my_new_window=
  107. {
  108.   50,            /* LeftEdge    x position of the window. */
  109.   25,            /* TopEdge     y positio of the window. */
  110.   320,           /* Width       320 pixels wide. */
  111.   100,           /* Height      100 lines high. */
  112.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  113.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  114.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  115.                  /*             user has selected the Close window gad, */
  116.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  117.   GADGETUP,      /*             a gadge has been released. */
  118.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  119.   WINDOWCLOSE|   /*             Close Gadget. */
  120.   WINDOWDRAG|    /*             Drag gadget. */
  121.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  122.   WINDOWSIZING|  /*             Sizing Gadget. */
  123.   ACTIVATE,      /*             The window should be Active when opened. */
  124.   &my_gadget,    /* FirstGadget A pointer to the String gadget. */
  125.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  126.   "Proportional Window", /* Title Title of the window. */
  127.   NULL,          /* Screen      Connected to the Workbench Screen. */
  128.   NULL,          /* BitMap      No Custom BitMap. */
  129.   320,           /* MinWidth    We will not allow the window to become */
  130.   50,            /* MinHeight   smaller than 320 x 50, and not bigger */
  131.   640,           /* MaxWidth    than 640 x 200. */
  132.   200,           /* MaxHeight */
  133.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  134. };
  135.  
  136.  
  137.  
  138. main()
  139. {
  140.   /* Boolean variable used for the while loop: */
  141.   BOOL close_me;
  142.  
  143.   /* Declare a variable in which we will store the IDCMP flag: */
  144.   ULONG class;
  145.  
  146.   /* Declare a pointer to an IntuiMessage structure: */
  147.   struct IntuiMessage *my_message;
  148.  
  149.  
  150.  
  151.   /* Before we can use Intuition we need to open the Intuition Library: */
  152.   IntuitionBase = (struct IntuitionBase *)
  153.     OpenLibrary( "intuition.library", 0 );
  154.   
  155.   if( IntuitionBase == NULL )
  156.     exit(); /* Could NOT open the Intuition Library! */
  157.  
  158.  
  159.  
  160.   /* We will now try to open the window: */
  161.   my_window = (struct Window *) OpenWindow( &my_new_window );
  162.   
  163.   /* Have we opened the window succesfully? */
  164.   if(my_window == NULL)
  165.   {
  166.     /* Could NOT open the Window! */
  167.     
  168.     /* Close the Intuition Library since we have opened it: */
  169.     CloseLibrary( IntuitionBase );
  170.  
  171.     exit();  
  172.   }
  173.  
  174.  
  175.  
  176.   /* We have opened the window, and everything seems to be OK. */
  177.  
  178.  
  179.  
  180.   close_me = FALSE;
  181.  
  182.   /* Stay in the while loop until the user has selected the Close window */
  183.   /* gadget: */
  184.   while( close_me == FALSE )
  185.   {
  186.     /* Wait until we have recieved a message: */
  187.     Wait( 1 << my_window->UserPort->mp_SigBit );
  188.  
  189.     /* We have now recieved one or more messages. */
  190.  
  191.     /* Since we may recieve several messages we stay in the while loop */
  192.     /* and collect, save, reply and execute the messages until there is */
  193.     /* a pause: */
  194.     while(my_message=(struct IntuiMessage *)GetMsg( my_window->UserPort))
  195.     {
  196.       /* GetMsg will return a pointer to a message if there was one, */
  197.       /* else it returns NULL. We will therefore stay in this while loop */
  198.       /* as long as there are some messages waiting in the port. */
  199.       
  200.       /* After we have collected the message we can read it, and save */
  201.       /* any important values which we maybe want to check later: */
  202.       class = my_message->Class;      /* Save the IDCMP flag. */
  203.  
  204.       /* After we have read it we reply as fast as possible: */
  205.       /* REMEMBER! Never try to read a message after you have replied! */
  206.       /* Some other process has maybe changed it. */
  207.       ReplyMsg( my_message );
  208.  
  209.       /* Check which IDCMP flag was sent: */
  210.       switch( class )
  211.       {
  212.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  213.                close_me=TRUE;
  214.                break;
  215.              
  216.         case GADGETDOWN:   /* The user has selected the Prop. gadget: */
  217.                printf("Proportional gadget selected.\n");
  218.                break;
  219.              
  220.         case GADGETUP:     /* The user has released the Prop. gadget: */
  221.                printf("Proportional gadget released.\n");
  222.                break;
  223.       }
  224.     }
  225.     printf("Volume= %1.0f\n\n", (float) my_prop_info.HorizPot/MAXPOT*64);
  226.   }
  227.  
  228.   /* We should always close the windows we have opened before we leave: */
  229.   CloseWindow( my_window );
  230.  
  231.  
  232.  
  233.   /* Close the Intuition Library since we have opened it: */
  234.   CloseLibrary( IntuitionBase );
  235.  
  236.   /* THE END */
  237. }
  238.  
  239. /*************************************************************************/
  240. /* EXTRA INFORMATION:                                                    */
  241. /* We will recieve a message (GADGETDOWN) when the user selects the      */
  242. /* knob, and one message (GADGETUP) when the user releases the knob. If  */
  243. /* the user on the other hand clicks inside the container (not on the    */
  244. /* knob) we will recieve both a GADGETDOWN and a GADGETUP message at the */
  245. /* same time.                                                            */
  246. /* It is because of that we need to have a while loop which collects the */
  247. /* messages once one or more has arrived. We can not as before just wait */
  248. /* and then collect one message, since there may be more in the queue.   */
  249. /*************************************************************************/
  250.